## Configure tempest
## -----------------

## * Clean up from any previous tempest run
## ::

# On doing tempest init workspace, it will create workspace directory
# as well as .workspace directory to store workspace information
# We need to delete .workspace directory otherwise tempest init failed
# to create tempest directory.
# We are doing this as sudo because tempest in containers create the files as
# root.
{% if tempest_format in ["venv", "packages"] %}
sudo rm -rf {{ working_dir }}/.tempest
sudo rm -rf {{ tempest_dir }}
{% endif %}


# Cloud Credentials
{% if tempest_os_cloud != '' %}
export OS_CLOUD={{ tempest_os_cloud }}
{% else %}
source {{ rc_file }}
{% endif %}

## Create Tempest resources
## ------------------------
## * For overcloud

{% if tempest_overcloud|bool  or tempest_os_cloud != '' %}

## * Clean up network if it exists from previous run
## ::

{% include 'cleanup-network.sh.j2' %}

{% endif %}

## * Ensure creator and Member role is present
## * Member role is needed for Heat tests.
## * creator role is needed for Barbican for running volume encryption tests.
## * ResellerAdmin role is needed for swift/RGW to grant object-store admin permissions.
## ::

openstack role show Member > /dev/null || openstack role create Member

openstack role show creator > /dev/null || openstack role create creator

openstack role show ResellerAdmin > /dev/null || openstack role create ResellerAdmin

## Install openstack-tempest
## -------------------------
## * Using git
## ::

{% if tempest_format == "venv" %}
# Create .venv in tempest_git directory with --system-site-packages to access tempest plugins
virtualenv --system-site-packages {{ working_dir }}/tempest_git/.venv
{{ working_dir }}/tempest_git/tools/with_venv.sh pip install -U pip
{{ working_dir }}/tempest_git/tools/with_venv.sh pip install -U setuptools
{{ working_dir }}/tempest_git/tools/with_venv.sh pip install {{ working_dir }}/tempest_git junitxml
# Install tempest plugins in tempest .venv
{% for tempest_plugin in (tempest_plugins_git|default([])) %}
{{ working_dir }}/tempest_git/tools/with_venv.sh pip install {{ working_dir }}/{{ tempest_plugin.replace('.git', '').split('/')[-1] }}
{% endfor %}

# Install python-tempestconf
{% if release != 'newton'%}
{{ working_dir }}/tempest_git/tools/with_venv.sh pip install {{ working_dir }}/tempestconf_git
{% endif %}
{% endif %}

{% if tempest_format == "container" %}
echo "========= Note: Executing tempest via a container =========="

cat <<'EOF' > {{ working_dir }}/tempest_container.sh
# Set the exit status for the command
set -e

# Load rc file or os_cloud file

{% if tempest_os_cloud == '' %}
source {{ rc_file_container }}
{% else %}
export OS_CLOUD="standalone"
cp -Rf /var/lib/tempest/.config ~/
{% endif %}

# Get the list of tempest/-plugins rpms installed within a tempest container
# It will be useful for debugging and making sure at what commit
# tempest/tempest plugins rpms are installed in the same.

rpm -qa | grep tempest

# Remove the existing tempest workspace
if [ -d {{ tempest_dir }}/etc ]; then
tempest workspace remove --name {{ tempest_dir | basename }} --rmdir
fi
{% endif %}

## Create Tempest Workspace
## ------------------------
## ::


# Create Tempest directory

mkdir {{ tempest_dir }}

# Create Tempest workspace
pushd {{ tempest_dir }}
{{ tempest_init }}
popd

## Generate tempest configuration using python-tempestconf
## -------------------------------------------------------
## ::
export TEMPESTCONF="{{ tempestconf }}"

{% if tempest_overcloud|bool or tempest_os_cloud != '' %}
# Get public net id

{% if release == 'newton' %}
public_net_id=$(neutron net-show {{ public_net_name }} -f value -c id)
{% else %}
{% if ironic_overcloud|default(false)|bool %}
public_net_id=$(openstack network show provisioning -f value -c id)
{% else %}
public_net_id=$(openstack network show {{ public_net_name }} -f value -c id)
{% endif %}
{% endif %}
{% endif %}

region_name=${OS_REGION_NAME:-'regionOne'}

{% if not tempest_overcloud|bool %}
# query the endpoint if not set (e.g. clouds.yaml)
export OS_AUTH_URL=${OS_AUTH_URL:-$(openstack endpoint list --service=identity --interface=public -c URL -f value)}
{% if release not in ['newton', 'ocata'] %}
# set the itentity api version if not set (e.g. clouds.yaml)
export OS_IDENTITY_API_VERSION=${OS_IDENTITY_API_VERSION:-3}
# OS_AUTH_URL does not contains identity api version in stackrc from
# Pike onwards.
export OS_AUTH_URL="$OS_AUTH_URL/v$OS_IDENTITY_API_VERSION"
{% elif release == 'pike' %}
# Use Identity version v2.0 for pike
export OS_AUTH_URL="$OS_AUTH_URL/v2.0"
{% endif %}
{% endif %}
# Generate Tempest Config file using python-tempestconf
# Notice aodh_plugin will be set to False if telemetry service is disabled
cd {{ tempest_dir }}
$TEMPESTCONF --out etc/tempest.conf \
  {% if tempest_overcloud|bool %}
  --deployer-input {{ tempest_data }}/{{ tempest_deployer_input_file }} \
  --network-id $public_net_id \
  {% endif %}
  {% if tempest_os_cloud != '' %}
  --os-cloud {{ tempest_os_cloud }} \
  --network-id $public_net_id \
  {% endif %}
  --image {{ tempest_test_image_path }} \
  --debug \
  {% if tempest_conf_removal and tempest_overcloud|bool %}
  {% for key, value in tempest_conf_removal.items() %}
  --remove {{ key }}={{ value }} \
  {% endfor %}
  {% endif %}
  --create \
  {% if tempest_format == 'container' %}
  DEFAULT.log_dir {{ tempest_log_dir }} \
  DEFAULT.log_file {{ tempest_log_file }} \
  {% endif %}
  identity.region $region_name \
  {% if release in ['newton', 'ocata', 'pike'] %}
  identity.uri $OS_AUTH_URL \
  identity.admin_password $OS_PASSWORD \
  identity.admin_username $OS_USERNAME \
  identity.disable_ssl_certificate_validation true \
  compute.allow_tenant_isolation true \
  scenario.img_file {{ tempest_test_image_name }} \
  {% else %}
  auth.use_dynamic_credentials true \
  network-feature-enabled.port_security true \
  {% endif %}
  {% if release in ['queens'] %}
  compute-feature-enabled.scheduler_available_filters RetryFilter,AvailabilityZoneFilter,ComputeFilter,ComputeCapabilitiesFilter,ImagePropertiesFilter,ServerGroupAntiAffinityFilter,ServerGroupAffinityFilter,PciPassthroughFilter \
  {% endif %}
  {% if release in ['newton', 'ocata', 'pike', 'queens'] %}
  compute-feature-enabled.attach_encrypted_volume False \
  compute-feature-enabled.console_output true \
  {% endif %}
  network.tenant_network_cidr {{ tenant_network_cidr }} \
  compute.build_timeout 500 \
  network.build_timeout 500 \
  volume.build_timeout 500 \
  service_available.aodh_plugin False \
  {% if tempest_extra_config %}
  {% for key, value in tempest_extra_config.items() %}
  {{ key }} {{ value }} \
  {% endfor %}
  {% endif %}
  orchestration.stack_owner_role Member \
  compute_feature_enabled.config_drive True

{% if ironic_overcloud|default(false)|bool %}
# tempest is using the m1.nano flavor, set it up for using a resource class if testing ironic
openstack flavor set m1.nano --property resources:VCPU=0 \
                             --property resources:MEMORY_MB=0 \
                             --property resources:CUSTOM_BAREMETAL=1 \
                             --property resources:DISK_GB=0
{% endif %}

### --stop_docs
